package com.yycx.module.bus.provider.handler.haiying.ackamount;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.security.OpenUser;
import com.yycx.module.bus.client.entity.BusCustom;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.client.entity.hy.HyOrderDetail;
import com.yycx.module.system.client.entity.SysCompany;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 已回款订单
 *
 * @author zyf
 */
@Component
public class HyReturnAmountTab3PageListHandler implements PageInterceptor<HyOrderDetail> {


    @Override
    public void prepare(CriteriaQuery<HyOrderDetail> cq, PageParams pageParams, EntityMap params) {
        cq.clear();
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate");
        Long saleManId = params.getLong("saleManId");
        OpenUser openUser = OpenHelper.getUser();
        Long companyId = openUser.getCompanyId();
        cq.addSelect(HyOrder.class, "orderId", "orderNo", "sendDate", "businessCode", "sendType","sendState", "customBondType", "saleManName");
        cq.addSelect(HyOrderDetail.class, "orderDetailsId","saleType", "deptId","parentId","drugsName","drugsSpec", "drugType", "salePrice", "saleNum", "saleAmount",  "customFloorAmount", "customBond", "customBondTakeAmount", "customBondDate", "deptPrice", "deptSettleAmount");
        cq.addSelect(BusCustom.class, "customName", "settleType");
        cq.addSelect(SysCompany.class, "companyShortName");
        cq.addSelect(BusDept.class, "deptName");
        //查询条件
        cq.eq("ho.saleManId", saleManId);
        cq.like(HyOrder.class, "orderNo");
        cq.like(BusCustom.class, "customName");
        cq.ge(FlymeUtils.isNotEmpty(beginDate), "bhd.sendDate", beginDate);
        cq.le(FlymeUtils.isNotEmpty(endDate), "bhd.sendDate", endDate);
        cq.eq(true, "ho.companyId", companyId);
        cq.eq(HyOrder.class, "deptId");
        //数据筛选条件
        cq.eq("bhd.customReturnState",CommonConstants.INT_1);
        //订单已完成审批
        cq.eq("ho.orderState", CommonConstants.INT_2);
        //关联表条件
        cq.createJoin(BusCustom.class);
        cq.createJoin(BusDept.class);
        cq.createJoin(SysCompany.class);
        cq.orderByAsc("ho.orderNo");
        cq.orderByDesc("ho.createTime");
    }
}
